package com.yunxi.controller.sto;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.yunxi.core.util.ExcelUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.sto.pojo.StoStockInPlanVO;
import com.yunxi.sto.pojo.VehiclePdiBillCheckVO;
import com.yunxi.sto.pojo.VehiclePdiBillListVO;
import com.yunxi.sto.pojo.VehiclePdiBillVO;
import com.yunxi.sto.service.VehiclePdiBillDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 车辆pdi订单 前端控制器
 * </p>
 *
 * @author caoYong
 * @since 2019-08-14
 */
@RestController
@RequestMapping("/admin/vehiclePdiBill")
@Api(tags = "sto:vehiclePdiBill", description = "车辆pdi订单接口")
public class VehiclePdiBillController {
    private static final Logger logger = LoggerFactory.getLogger(VehiclePdiBillController.class);

    @Autowired
    VehiclePdiBillDubboService vehiclePdiBillDubboService;

    @GetMapping("/page")
    @ApiOperation(value = "查询分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
    })
    public ApiResult page(VehiclePdiBillVO vehiclePdiBillVO, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {
        vehiclePdiBillVO.setPartnerCode(loginInfo.getPartnerCode());
        return ResponseUtil.ok(vehiclePdiBillDubboService.findPageByVO(vehiclePdiBillVO, pageNum, pageSize));
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public ApiResult export(VehiclePdiBillVO vehiclePdiBillVO,@CurrentUser LoginInfo loginInfo) throws Exception {
        vehiclePdiBillVO.setPartnerCode(loginInfo.getPartnerCode());
        return ResponseUtil.ok(vehiclePdiBillDubboService.export(vehiclePdiBillVO));
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询列表")
    public ApiResult list(VehiclePdiBillVO vehiclePdiBillVO,@CurrentUser LoginInfo loginInfo) throws Exception {
        vehiclePdiBillVO.setPartnerCode(loginInfo.getPartnerCode());
        return ResponseUtil.ok(vehiclePdiBillDubboService.listByVo(vehiclePdiBillVO));
    }

    @GetMapping("/load")
    @ApiOperation(value = "读取单个")
    @ApiImplicitParam(paramType="query", name = "pdiBillId", value = "主键id", required = true, dataType = "String")
    public ApiResult load(String pdiBillId) throws Exception {
        return ResponseUtil.ok(vehiclePdiBillDubboService.getById(pdiBillId));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存信息")
    public ApiResult save(@RequestBody VehiclePdiBillVO vehiclePdiBillVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        vehiclePdiBillVO.setPartnerCode(loginInfo.getPartnerCode());
        //PDI见
        vehiclePdiBillVO.setCreateTime(new Date());
        vehiclePdiBillVO.setCreateBy(loginInfo.getRealName());
        vehiclePdiBillVO.setUpdateTime(new Date());
        vehiclePdiBillVO.setUpdateBy(loginInfo.getRealName());
        vehiclePdiBillVO.setChecker(loginInfo.getUserCode());

        String pdiBillId = vehiclePdiBillDubboService.saveOrUpdate(vehiclePdiBillVO);
        return ResponseUtil.ok(pdiBillId);
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public ApiResult del(@RequestBody List<VehiclePdiBillVO> vehiclePdiBillVOList) throws Exception {
        vehiclePdiBillDubboService.removeByListVo(vehiclePdiBillVOList);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/check")
    @ApiOperation(value = "PDI检查")
    public ApiResult check(@RequestBody VehiclePdiBillCheckVO vehiclePdiBillCheckVO,@CurrentUser LoginInfo loginInfo) throws Exception {
        vehiclePdiBillCheckVO.setChecker(loginInfo.getUserCode());
        vehiclePdiBillDubboService.checkToMQ(vehiclePdiBillCheckVO);
        return ResponseUtil.ok("success");
    }

    @GetMapping("/pdiList")
    @ApiOperation(value = "获取PDI检查项")
    public ApiResult pdiList() throws Exception {
        return ResponseUtil.ok(vehiclePdiBillDubboService.pdiList());
    }

    @GetMapping("/pdiBillList")
    @ApiOperation(value = "通过VIN码查询PDI检查单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "vin", value = "VIN码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "isComplete", value = "是否完成(1是,0否)", required = false, dataType = "String")
    })
    public ApiResult pdiBillList(String vin,String isComplete) throws Exception {
        return ResponseUtil.ok(vehiclePdiBillDubboService.pdiBillList(vin,isComplete));
    }

    @PostMapping("/print")
    @ApiOperation(value = "打印标签")
    public ApiResult print(@RequestBody StoStockInPlanVO stoStockInPlanVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        vehiclePdiBillDubboService.printStockInImg(stoStockInPlanVO);
        return ResponseUtil.ok("success");
    }

    @GetMapping("/exportPdiDetail")
    @ApiOperation(value = "导出PDI详情")
    @ApiImplicitParam(paramType="query", name = "pdiBillId", value = "主键id", required = true, dataType = "String")
    public ModelAndView exportPdi(String pdiBillId, HttpServletResponse response) throws Exception {
        VehiclePdiBillVO vehiclePdiBillVO = vehiclePdiBillDubboService.exportPdi(pdiBillId);
        List<VehiclePdiBillListVO> detailList = vehiclePdiBillVO.getDetailList();
        Workbook sheets = ExcelExportUtil.exportExcel(new ExportParams(vehiclePdiBillVO.getVin(), vehiclePdiBillVO.getVin()+" PDI.xlsx"), VehiclePdiBillListVO.class, detailList);
        try {
            this.setResponseHeader(response, sheets.getSheetName(0));
            OutputStream os = response.getOutputStream();
            sheets.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/exportLoad")
    @ApiOperation(value = "导出详情")
    @ApiImplicitParam(paramType="query", name = "pdiBillId", value = "主键id", required = true, dataType = "String")
    public ModelAndView exportLoad(String pdiBillId, HttpServletResponse response) throws Exception {
        String[][] content = vehiclePdiBillDubboService.exportLoad(pdiBillId);
        String sheetName = "test";
        String fileName = "testFileName.xlsx";
        String [] title = {"检查顺序","项目名称","检查标准","检查方法","是否合格","描述"};
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null,null,null);
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
